package edu.gatech.cs2340.teamrocket.control.guicontrol;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.awt.event.WindowEvent;
import edu.gatech.cs2340.teamrocket.model.Model;
import edu.gatech.cs2340.teamrocket.view.GameFrame;
import edu.gatech.cs2340.teamrocket.view.MainMenu;
import edu.gatech.cs2340.teamrocket.view.networking.NetworkSetupGUIInterface;
import edu.gatech.cs2340.teamrocket.control.guicontrol.networking.NetworkSetupControl;
import edu.gatech.cs2340.teamrocket.control.guicontrol.networking.NetworkSetupControlInterface;
import edu.gatech.cs2340.teamrocket.view.networking.NetworkSetupGUI;
import javax.swing.JOptionPane;

/**
 *
 * @author Wai Chip Ngai
 *
 * This is the controller for the main menu at the very beginning of the game.
 *
 */
public class MainMenuControl {

    private GameFrame mainFrame;
    private MainMenu aMenu;

    /**
     * It makes a GUI to let the user to start the game, load game, or exit the game.
     * @param mainFrame the main Game Frame
     */
    public MainMenuControl(GameFrame mainFrame){
        this.mainFrame = mainFrame;
        aMenu = new MainMenu(this);
        mainFrame.add(aMenu, GameFrame.MAIN_MENU_JPANEL);
        mainFrame.showPanel(GameFrame.MAIN_MENU_JPANEL);
    }
    /**
     * Exit the program when the user chooses to quit game
     */
    public void quitGame(){
        mainFrame.dispatchEvent(new WindowEvent(mainFrame, WindowEvent.WINDOW_CLOSING));
    }
    /**
     * Start a single player game
     */
    public void startSingleGame(){
        new IntroScreenControl(mainFrame);

    }

    /**
     * Start a multiplayer game;
     */
    public void startMultiPlayer(){

        NetworkSetupControlInterface control = new NetworkSetupControl(new Runnable(){
            public void run(){
                startSingleGame();
            }
        });
        NetworkSetupGUIInterface gui = new NetworkSetupGUI(control);

        gui.createAndShowGUI();
    }

    /**
     * Load a previous game;
     */
    public void loadGame(){
        try{
            Model model = Model.getInstance();
            model.loadGameData();
            MainGamePanelControl control = new MainGamePanelControl(mainFrame);
            control.setUpdatesLabel(model.getUpdates());
            mainFrame.showPanel(GameFrame.MAIN_GAME_JAPNEL);
        }
        catch(FileNotFoundException e){
            JOptionPane.showMessageDialog(mainFrame, "The save file could not be found. The game was not loaded.");
        }
        catch(IOException e){
            JOptionPane.showMessageDialog(mainFrame, "There was an IO problem. The game was not loaded.");
        }
    }
}